package com.dz.Incubator.controller;

import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.config.ReturnCode;
import com.dz.Incubator.dto.ProblemDto;
import com.dz.Incubator.entity.InvokeResult;
import com.dz.Incubator.service.ProblemService;
import com.dz.Incubator.entity.ProblemEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api/a/zx/problem")
@Slf4j
public class ProblemController {


    @Autowired
    private ProblemService problemService;

    /**
     * 分页查询 problem 对象
     *
     * @param pagination 条件
     */
    @RequestMapping(value = "/getPageList", method = RequestMethod.POST)
    @ResponseBody
    public InvokeResult<Pagination> getPageList(@RequestBody Pagination pagination) {
        InvokeResult invokeResult;
        try {
            if (null == pagination) {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
            Pagination pageInfo = problemService.findByPagination(pagination);
            if (null != pageInfo && null != pageInfo.getList() && pageInfo.getList().size() > 0) {
                invokeResult = InvokeResult.success(pageInfo);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 获取单个ProblemEntity 对象
     *
     * @param id Id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<ProblemEntity> get(@PathVariable Long id) {
        InvokeResult invokeResult = null;
        try {
            if (null != id) {
                ProblemEntity problemEntity = problemService.get(id);
                if (null == problemEntity) {
                    invokeResult = InvokeResult.pass_no_data();
                }
                invokeResult = InvokeResult.success(problemEntity);
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 删除单个problemEntity 对象
     *
     * @param id 删除数据 ID
     */
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult delete(@RequestParam Long id) {
        InvokeResult invokeResult = null;
        try {
            if (null != id) {
                problemService.delete(id);
                invokeResult = InvokeResult.success();
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 集合查找 problemEntity 对象
     *
     */
    @RequestMapping(value = "/find", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<List<ProblemEntity>> find(@RequestParam("ids") List<Long> ids) {
        InvokeResult invokeResult = null;
        try {
            if (null != ids && ids.size() > 0) {
                Long[] skuIds = ids.toArray(new Long[ids.size()]);
                List<ProblemEntity> problemEntitys = problemService.find(skuIds);
                if (null != problemEntitys && problemEntitys.size() > 0) {
                    invokeResult = InvokeResult.success(problemEntitys);
                } else {
                    invokeResult = InvokeResult.pass_no_data();
                }
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 查找所有 problemEntity 对象
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<ProblemEntity>> findAll() {
        InvokeResult invokeResult = null;
        try {
            List<ProblemEntity> problemEntitys = problemService.findAll();
            if (null != problemEntitys && problemEntitys.size() > 0) {
                invokeResult = InvokeResult.success(problemEntitys);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 新增数据
     *
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<ProblemEntity> add(@RequestBody ProblemEntity problemEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null != problemEntity) {
                invokeResult = InvokeResult.success(problemService.insert(problemEntity));
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 修改数据
     *
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<ProblemEntity> update(@RequestBody ProblemEntity problemEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null != problemEntity && null != problemEntity.getId()) {
                invokeResult = InvokeResult.success(problemService.update(problemEntity));
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 查询常见问题信息
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<ProblemDto>> getInfo() {
        InvokeResult invokeResult;
        try {
            List<ProblemDto> problemDtos = problemService.getInfo();
            if (null != problemDtos && problemDtos.size() > 0) {
                invokeResult = InvokeResult.success(problemDtos);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }


    /**
     * 通过服务查询问题列表
     */
    @RequestMapping(value = "/service", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<ProblemDto>> getInfoByService(Long serviceId) {
        InvokeResult invokeResult;
        try {
            if (null != serviceId) {
                List<ProblemDto> problemInfoDto = problemService.getInfoByService(serviceId);
                if (null == problemInfoDto) {
                    invokeResult = InvokeResult.pass_no_data();
                }
                invokeResult = InvokeResult.success(problemInfoDto);
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    @RequestMapping(value = "getTypeAndProblem", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<ProblemEntity>> getTypeAndProblem() {
        return InvokeResult.success(problemService.getTypeAndProblem());
    }

}